package com.mapmymotion;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;

public class SQLList{
	/** Called when the Sql list is first created. */
	private static JSONArray jArrayFriends = null;
	private static JSONArray jArrayActivities = null;
	private static JSONArray jArrayIntervals = null;
	private static JSONArray jArrayPoints = null;
	
	
	
	public static void getData(Integer aType,String aId) {
		InputStream is = null;
		String result = "";
		String sUrl = null;
		// the year data to send
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		switch(aType){
		case Constants.FRIENDINFO:
			nameValuePairs.add(new BasicNameValuePair("memberid", aId));
			nameValuePairs.add(new BasicNameValuePair("myoption", "0"));
			sUrl ="http://mapmymotion.com/getfriends.php";
			break;
		case Constants.INTERVALINFO:
			nameValuePairs.add(new BasicNameValuePair("activitysummaryid", aId));
			nameValuePairs.add(new BasicNameValuePair("myunits", "1609.344"));
			sUrl="http://mapmymotion.com/getintervals.php";
			break;
		case Constants.ACTIVITIES:
			nameValuePairs.add(new BasicNameValuePair("memberid", aId));
			nameValuePairs.add(new BasicNameValuePair("friends", "0"));
			sUrl = "http://mapmymotion.com/getactivities.php";
			break;
		case Constants.POINTS:
		nameValuePairs.add(new BasicNameValuePair("activitysummaryid", aId));
		sUrl = "http://mapmymotion.com/getpoints3.php";
		break;
		}
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(sUrl);

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
			

		} catch (Exception e) {

			Log.e("log_tag", "Error in http connection " + e.toString());
		}

		// convert response to string
		try {

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;

			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();

			result = sb.toString();
			
		} catch (Exception e) {

			Log.e("log_tag", "Error converting result " + e.toString());
		}
		// parse json data
		try {
			switch(aType){
			case Constants.FRIENDINFO:
				jArrayFriends = new JSONArray(result);
				break;
			case Constants.ACTIVITIES:
				jArrayActivities = new JSONArray(result);
				break;
			case Constants.INTERVALINFO:
				jArrayIntervals = new JSONArray(result);
				break;
			case Constants.POINTS:
				jArrayPoints = new JSONArray(result);
				break;
			}
		} catch (JSONException e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
		}

	}
	public static JSONArray getActivities(){
		return jArrayActivities;
	}

	public static JSONArray getFriends(){
		return jArrayFriends;
	}
	public static JSONArray getIntervals(){
		return jArrayIntervals;
	}
	public static JSONArray getPoints(){
		return jArrayPoints;
	}

}
